package com.xsdl.iterator;

import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

@Data
@ToString
public class User implements Iterable<String> {

    private String uuid;

    private String name;

    private Integer age;

    @Override
    public Iterator<String> iterator() {
        return new UserIte();
    }

    private class UserIte implements Iterator<String> {

        private Integer index;

        private List<String> propertyList = new ArrayList<>();

        public UserIte() {
            index = 0;
            propertyList.add(User.this.uuid);
            propertyList.add(User.this.name);
            propertyList.add(User.this.age + "");
        }

        @Override
        public boolean hasNext() {
            return index < propertyList.size();
        }

        @Override
        public String next() {
            if (index >= propertyList.size()) {
                throw new NoSuchElementException("没有" + index + "对应的元素");
            }
            return propertyList.get(index++);
        }
    }

}
